// 基础数据-仓库相关-库区管理-zhangxu
import React, { Component } from 'react'
import {ListBox} from '../../../components'
import { Modal,message } from 'antd';
import AreaForm from './AreaFrom'
import {fetchData} from '../../../components/AjaxConsumer'
const confirm = Modal.confirm;

export default class AreaManager extends Component{
    constructor(props) {
        super(props);
        this.state={
            loading: false,
            visible: false,
            addFlag: false,
            formData:{},
            submitUrl:'',
        };
        //行操作列表-已弃用
        this.rowOperation = [
            
        ];
        //header 操作列表111
        this.headerOperation = [
            {
                handleName: 'addArea',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        // foreverHidden永久隐藏
        this.tableColumn = {
            storageKey: 'ListAreaColumnConfig',
            data: [
                {
                    showName: 'id',
                    columnName: 'areaId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '库区编码',
                    columnName: 'areaCode',
                    columnWidth:120
                },{
                    showName: '库区描述',
                    columnName: 'areaDesc',
                    columnWidth:200
                },{
                    showName: '区域编码',
                    columnName: 'regionCode',
                    columnWidth:130
                },{
                    showName: '仓库编码',
                    columnName: 'whCode'
                },
                // {
                //     showName: '入库过渡库位',
                //     columnName: 'instockTranLoc',
                //     columnWidth:130
                // },{
                //     showName: '出库过渡库位',
                //     columnName: 'outstockTranLoc',
                //     columnWidth:130
                // },
                //{
                //    showName: 'sap库存地点',
                //    columnName: 'sapSendSspot',
                //    columnWidth:130
                //},
                //// {
                ////     showName: '拣货过渡库位',
                ////     columnName: 'pickTranLoc',
                ////     columnWidth:130
                //// },
                //{
                //    showName: '默认异常储位',
                //    columnName: 'exSignLocNo',
                //    columnWidth:150
                //},{
                //    showName: '默认隔离储位',
                //    columnName: 'frozenLocNo',
                //    columnWidth:150
                //},
                //// {
                ////     showName: ' 库区基点',
                ////     columnName: 'areaPoint'
                //// },
                //{
                //    showName: '使用托盘',
                //    columnName: 'useTray',
                //    showOption:[
                //    {
                //        optionKey:'',
                //        optionShow:''
                //    },{
                //        optionKey:'0',
                //        optionShow:'否'
                //    },{
                //        optionKey:'1',
                //        optionShow:'是'
                //    }
                //]
                //},{
                //    showName: '托盘混载',
                //    columnName: 'trayMix',
                //    showOption:[
                //        {
                //            optionKey:'',
                //            optionShow:''
                //        },{
                //            optionKey:'0',
                //            optionShow:'否'
                //        },{
                //            optionKey:'1',
                //            optionShow:'是'
                //        }
                //    ]
                //},{
                //    showName: '入库策略',
                //    columnName: 'instorageStrategy',
                //    showOption:[
                //        {
                //            optionKey:'',
                //            optionShow:''
                //        },{
                //            optionKey:'1',
                //            optionShow:'一步'
                //        },{
                //            optionKey:'2',
                //            optionShow:'两步'
                //        }
                //    ]
                //},{
                //    showName: '出库策略',
                //    columnName: 'outstorageStrategy',
                //    showOption:[
                //        {
                //            optionKey:'',
                //            optionShow:''
                //        },{
                //            optionKey:'1',
                //            optionShow:'一步'
                //        },{
                //            optionKey:'2',
                //            optionShow:'两步'
                //        }
                //    ]
                //},
                //// {
                ////     showName: '是否强制出库',
                ////     columnName: 'forceOutstorage',
                ////     columnWidth:130,
                ////     showOption:[
                ////         {
                ////             optionKey:'',
                ////             optionShow:''
                ////         },{
                ////             optionKey:'1',
                ////             optionShow:'是'
                ////         },{
                ////             optionKey:'0',
                ////             optionShow:'否'
                ////         }
                ////     ]
                //// },
                //{
                //    showName: '库位允许拣货',
                //    columnName: 'locPickFlag',
                //    columnWidth:130,
                //    showOption:[
                //        {
                //            optionKey:'',
                //            optionShow:''
                //        },{
                //            optionKey:'1',
                //            optionShow:'是'
                //        },{
                //            optionKey:'0',
                //            optionShow:'否'
                //        }
                //    ]
                //},{
                //    showName: '默认分配规则',
                //    columnName: 'allocationRule',
                //    columnWidth:150
                //},
                //// {
                ////     showName: '默认集货位推荐类型',
                ////     columnName: 'gatherLocType',
                ////     columnWidth:160
                //// },
                //{
                //    showName: '优先级',
                //    columnName: 'priority'
                //},
                {
                    showName: '激活标记',
                    columnName: 'activeFlag',
                    showOption:[
                        {
                            optionKey:'1',
                            optionShow:'启用'
                        },{
                            optionKey:'2',
                            optionShow:'禁用'
                        }
                    ]
                },{
                    showName: '创建人',
                    columnName: 'createBy',
                    columnWidth:120
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnWidth:140
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy',
                    columnWidth:120
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified',
                    columnWidth:140
                }
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            {
                name:'useTray',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                name:'trayMix',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                name: 'instorageStrategy',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'一步'
                    },{
                        optionKey:'2',
                        optionShow:'两步'
                    }
                ]
            },{
                name: 'outstorageStrategy',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'一步'
                    },{
                        optionKey:'2',
                        optionShow:'两步'
                    }
                ]
            },
            // {
            //     name: 'forceOutstorage',
            //     type: 'select',
            //     optionList:[
            //         {
            //             optionKey:'',
            //             optionShow:''
            //         },{
            //             optionKey:'1',
            //             optionShow:'是'
            //         },{
            //             optionKey:'0',
            //             optionShow:'否'
            //         }
            //     ]
            // },
            {
                name: 'locPickFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },{
                name: 'priority',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'1'
                    },{
                        optionKey:'2',
                        optionShow:'2'
                    },{
                        optionKey:'3',
                        optionShow:'3'
                    },{
                        optionKey:'4',
                        optionShow:'4'
                    }
                ]
            },{
                name:'activeFlag',
                type: 'select',
                optionList:[
                    {
                            optionKey:'',
                            optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'启用'
                    },{
                        optionKey:'2',
                        optionShow:'禁用'
                    }
                ]
            },{
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax', //最大值， 选择小于时使用；选择范围时大值使用
                minName:'gmtCreateMin'  //最小值， 选择大于时使用；选择范围时小值使用
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax', 
                minName:'gmtModifiedMin'
            }
        ];

    }

// 行操作
    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'addArea'){
            this.addHandle();
        }
    }

    addHandle = () => {
        this.setState({
            formData:{},
            submitUrl:'/base/data/area/c'
        })
        this.showModal();
    }

    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            let id = rowObj[0].data.areaId;
            let options={
                url:'/base/data/area/r/'+id,
                condition:{}
            };

            fetchData(this, options, (resData) => {
                if (resData.code == '1'){
                    this.setState({
                        formData:resData.data,
                        submitUrl:'/base/data/area/u'
                    });
                    this.showModal();
                }else {
                    message.error(resData.msg);
                }
            });
        }
    }

    deleteHandle = (rowObj) => {
        console.log('-----',rowObj);
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            console.log('--shanchu---',rowObj);
            let areaId = rowObj[0].data.areaId;
            console.log(areaId);
            let options= {
                url: '/base/data/area/d',
                condition:{areaId:areaId}
            };
            let _that = this;
            confirm({
                title: '您确定要删除吗？',
                onOk() {
                    fetchData(_that, options, (resData) => {
                        console.log(resData);
                        _that.setState({
                            resData: resData
                        });
                        if (resData.code == '1'){
                            message.success('删除成功！');
                            _that.refreshHandle();
                        }else {
                            message.error(resData.msg);  
                        }
                    });
                },
                onCancel() {
                    console.log("取消了删除操作")
                },
            });
        }
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.areaList.refreshHandle();
    }

    showModal = () => {
        this.setState({
            visible: true
        });
    }

    handleCancel = () => {
        this.setState({
            visible: false
        });
        this.refreshHandle();
    }
    

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'areaList'
                    isHideLeft={isHideLeft}
                    title=" 库区维护"
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/area/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />

                <Modal
                    style={{top: '20px'}}
                    visible={this.state.visible}
                    title="库区维护"
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                    width={'850px'}
                    footer=''
                    maskClosable={false}>
                    <AreaForm
                        onCancel={this.handleCancel}
                        ref="areaFrom"
                        submitUrl={this.state.submitUrl}
                        formData={this.state.formData}
                        init={this.state.visible}
                        refreshHandle={this.refreshHandle}
                    />
                </Modal>
            </div>
        )
    }

}
